package com.balance.anole.erp.report.action;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.WorkbenchException;

import com.balance.anole.erp.constant.IIcons;
import com.balance.anole.erp.report.view.ReportDemoPreView;
import com.balance.anole.erp.report.view.ReportDemoView;
import com.balance.anole.erp.util.Messages;

public class ReportDemoPreAction extends Action{
	private ReportDemoView view;

	private IWorkbenchWindow window;
	
	public void setView(ReportDemoView view) {
		this.view = view;
	}
	
	public void setWindow(IWorkbenchWindow window) {
		this.window = window;
	}
	
	public ReportDemoPreAction(ReportDemoView view){
		setView(view);
		setWindow(view.getSite().getWorkbenchWindow());
		setId("com.balance.anole.erp.report.demo1");
		setToolTipText("pdf view");
		
	}
	
	public void run(){
		try {
			//genHtmlReport();
			window.getActivePage().showView(ReportDemoPreView.ID);
		} catch (WorkbenchException e) {
			 MessageDialog.openError(view.getSite().getShell(), Messages.DIALOG_INFO_ERROR, e.getMessage());
		} 
//		catch (JRException e){
//			MessageDialog.openError(view.getSite().getShell(), Messages.DIALOG_INFO_ERROR, e.getMessage());
//		}
	}
	
	public void setEnabled(boolean enabled){
		super.setEnabled(enabled);
		if(enabled){
			setImageDescriptor(com.balance.anole.erp.Activator.getImageDescriptor(IIcons.PRINT));		
		}else{
			setImageDescriptor(com.balance.anole.erp.Activator.getImageDescriptor(IIcons.DISABLE_PRINT));
		}
	}
	
	private void genHtmlReport() throws JRException{
		//JasperFillManager.fillReportToFile("C:\\Documents and Settings\\zhouxiaobo\\workspace\\Anole-client\\bin\\report1.jasper", null, new JREmptyDataSource());
		//JasperExportManager.exportReportToHtmlFile("C:\\Documents and Settings\\zhouxiaobo\\workspace\\Anole-client\\bin\\report1.jrprint","C:\\reportTemp\\DemoReport.html");
		fill();
		html();
	}
	
	public void fill() throws JRException {
		long start = System.currentTimeMillis();
		JasperFillManager.fillReportToFile("C:\\Documents and Settings\\zhouxiaobo\\workspace\\Anole-client\\report\\report1.jasper", null, new JREmptyDataSource());
		System.err.println("Filling time : " + (System.currentTimeMillis() - start));
	}
	
	public void html() throws JRException {
		long start = System.currentTimeMillis();
		JasperExportManager.exportReportToHtmlFile("C:\\Documents and Settings\\zhouxiaobo\\workspace\\Anole-client\\report\\report1.jrprint","C:\\reportTemp\\DemoReport.html");
		System.err.println("HTML creation time : " + (System.currentTimeMillis() - start));
	}
}
